home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / OBJARRAY.CPP < prev    next >
Text File  |  1991-04-28  |  2KB  |  81 lines

  1.                                  // Chapter 6 - Program 1
  2. #include "iostream.h"
  3.  
  4. class box {
  5.    int length;
  6.    int width;
  7.    static int extra_data;
  8. public:
  9.    box(void);             //Constructor
  10.    void set(int new_length, int new_width);
  11.    int get_area(void);
  12.    int get_extra(void) {return extra_data++;}
  13. };
  14.  
  15.  
  16. box::box(void)        //Constructor implementation
  17. {
  18.    length = 8;
  19.    width = 8;
  20.    extra_data = 1;
  21. }
  22.  
  23.  
  24. // This method will set a box size to the two input parameters
  25. void box::set(int new_length, int new_width)
  26. {
  27.    length = new_length;
  28.    width = new_width;
  29. }
  30.  
  31.  
  32. // This method will calculate and return the area of a box instance
  33. int box::get_area(void)
  34. {
  35.    return (length * width);
  36. }
  37.  
  38.  
  39. main()
  40. {
  41. box small, medium, large, group[4];        //Seven boxes to work with
  42.  
  43.    small.set(5, 7);
  44.    large.set(15, 20);
  45.    
  46.    for (int index = 1 ; index < 4 ; index++)  //group[0] uses default
  47.       group[index].set(index + 10, 10);
  48.  
  49.    cout << "The small box area is " << small.get_area() << "\n";
  50.    cout << "The medium box area is " << medium.get_area() << "\n";
  51.    cout << "The large box area is " << large.get_area() << "\n";
  52.  
  53.    for (index = 0 ; index < 4 ; index++)
  54.       cout << "The array box area is " << 
  55.                                      group[index].get_area() << "\n";
  56.  
  57.    cout << "The extra data value is " << small.get_extra() << "\n";
  58.    cout << "The extra data value is " << medium.get_extra() << "\n";
  59.    cout << "The extra data value is " << large.get_extra() << "\n";
  60.    cout << "The extra data value is " << group[0].get_extra() << "\n";
  61.    cout << "The extra data value is " << group[3].get_extra() << "\n";
  62. }
  63.  
  64.  
  65.  
  66.  
  67. // Result of execution
  68. //
  69. // The small box area is 35
  70. // The medium box area is 64
  71. // The large box area is 300
  72. // The array box area is 64
  73. // The array box area is 110
  74. // The array box area is 120
  75. // The array box area is 130
  76. // The extra data value is 1
  77. // The extra data value is 2
  78. // The extra data value is 3
  79. // The extra data value is 4
  80. // The extra data value is 5
  81.